﻿using UnityEngine;
using System.Collections;

public class Controls : MonoBehaviour {
	
	// Use this for initialization
	private bool mouseDown = false;
	public GameObject selectedObject;
	public GameObject[] colours;
	public Camera cam;
	void Start () {
		colours = GameObject.FindGameObjectsWithTag("Bar");
		cam.aspect = 16.0f/9.0f;
	}
	void AllControls()
	{
		mouseDown = true;
		Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
		RaycastHit hit;
		
		if( Physics.Raycast( ray, out hit, 100 ) )
		{
			if(hit.transform.gameObject.tag == "Block")
				selectedObject = hit.transform.gameObject;
			
		}
	}
	void WindowsControls()
	{
		if( Input.GetMouseButtonDown(0) )
		{
			AllControls();
		}
		if(Input.GetMouseButtonUp(0) )
		{
			mouseDown = false;
			selectedObject = null;
		}
		
		
	}
	void AndroidControls()
	{
		foreach (Touch touch in Input.touches) 
		{
			if(touch.phase == TouchPhase.Began)
			{
				AllControls();
			}
			if(touch.phase == TouchPhase.Ended)
			{
				mouseDown = false;
				selectedObject = null;
			}
		}
	}
	float RoundToNearestColour(float hitY)
	{
		float closest = 1000;
		float temp = 0;
		foreach (var colourBar in colours) {
			float distance = colourBar.transform.position.y - hitY;
			
			if(distance*distance < closest*closest)
			{ temp = colourBar.transform.position.y; closest = distance; }
		}
		return temp;
	}
	// Update is called once per frame
	void Update () {
	//	WindowsControls();
	/*	if(mouseDown)
		{
			RaycastHit hit;
			if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 500.0f))
				
			{	
				if(selectedObject != null)
				{
					
					selectedObject.transform.position = new Vector3(selectedObject.transform.position.x,RoundToNearestColour(hit.point.y),selectedObject.transform.position.z);
				}
				
			}
			
			
		}*/
	}
}
